<template>
	<view>
		<view class="">
			<view class="margin-lr margin-top" style="position: relative;border-radius: 10upx;overflow: hidden;">
				<image src="../static/vip.png" style="width: 100%;height: 250rpx;"></image>
				<view class=" u-flex u-p-l-30 u-p-t-80 u-p-b-30" style="position: absolute;top: 0;width: 100%;">
					<view class="u-m-r-20">
						<u-avatar :src="avatar" size="100"></u-avatar>
					</view>
					<view class="u-flex-1 ">
						<view class="u-font-18 text-white text-bold">{{userName}}</view>

						<view class="text-26 u-m-t-10 u-tips-color" style="color: #FFF;" v-if="isVip==1 && isVipEnd">
							到期时间:{{isVipEnd}}
						</view>
						<view class="u-font-14 u-m-t-10 u-tips-color" style="color: #FFF;" v-else>您目前还未开通会员
						</view>
					</view>
				</view>
			</view>
			<view class="flex margin-top-sm flex-wrap padding-lr-sm">
				<view v-for="(item,index) in vipList" :key='index' @click="select(item)" class=""
					style="display: inline-block;width: 216rpx;height: 250rpx;margin: 10rpx 10rpx;">
					<view class="text-center flex flex-direction justify-between padding-tb radius"
						:class="selNum == item.id?'active': ''"
						style="color: #dfc5a7;width: 216rpx;height: 250rpx;background:#291B3D;border: 1px; ">
						<view class="text-bold">{{item.vipName}}</view>
						<view class="text-bold">￥<text class="text-xxl">{{item.money}}</text></view>
						<view class="text-sm">立即购买</view>
					</view>
				</view>
			</view>
			<view class="vipbox">
				<view class="text-center text-xl text-bold " style="color:#dfc5a7;">会员特权</view>
				<view class="flex  flex-wrap">
					<view v-for="(item,index) in MemberList" :key="index"
						style="width: 33%;text-align: center;margin-top: 34upx;">
						<image :src="item.memberImg" mode="" style="margin: 0 auto;height: 45rpx;width: 45rpx;"></image>
						<view class="grid-text margin-top-sm" style="color: #dfc5a7;">{{item.memberName}}</view>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 110rpx;"></view>
		<view class="flex justify-between cu-bar foot bg padding-lr">
			<view style="color: #AC75FE;">
				实付：<text style="font-size: 38upx;margin-top: 8upx;margin-right: 2upx;">{{price}}</text>币
			</view>
			<view class="">
				<u-button :custom-style="customStyle" @click="pay()" shape="circle"
					:hair-line="false">{{isVip==1?'立即续费':'立即开通'}}</u-button>
			</view>
		</view>
		<popup :show="popupshow" @closeT="popupshow = false" @sureT="sureT" :content="content"></popup>
	</view>
</template>

<script>
	import popup from '@/components/popup.vue'
	export default {
		components: {
			popup
		},
		data() {
			return {
				popupshow: false,
				content: '',


				customStyle: {
					width: '250rpx',
					color: '#FFFFFF',
					background: "#AC75FE",
					border: 0,
					fontWeight: '700'
				},
				gridData: [{
						title: '专享折上折',
						image: '../static/1.png'
					},
					{
						title: '特权礼物',
						image: '../static/2.png'
					},
					{
						title: '身份标识',
						image: '../static/3.png'
					},
					{
						title: '超值专享券',
						image: '../static/4.png'
					},
					{
						title: '商家特权',
						image: '../static/5.png'
					},
					{
						title: '定制挂件',
						image: '../static/6.png'
					}
				],
				avatar: '',
				userName: '匿名',
				vipList: [],
				selNum: '',
				newPrice: 0,
				money: 0,
				price: 0,
				MemberList: [],
				isVip: 1,
				isVipEnd: ''
			}
		},
		onLoad() {
			this.avatar = uni.getStorageSync('avatar')
			this.userName = uni.getStorageSync('userName')
			this.isVip = 1
			this.getVipList()
			this.getMemberList()
			this.getMoney()
			this.getIsVip()
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				this.$Request.get("/app/user/selectUserById").then(res => {
					if (res.code == 0) {
						this.avatar = res.data.avatar ? res.data.avatar : '../../static/logo.png'

						uni.setStorageSync('avatar', res.data.avatar)
					}
				});
			},
			//获取VIP列表
			getVipList() {
				this.$Request.get('/app/VipDetails/selectVipDetails').then(res => {
					if (res.code == 0) {
						this.vipList = res.data
						this.price = this.vipList[0].money
					}
				})
			},
			// 获取特权列表
			getMemberList() {
				this.$Request.get('/app/member/selectMemberList').then(res => {
					if (res.code == 0) {
						this.MemberList = res.data
					}
				})
			},
			getIsVip() {
				this.$Request.get("/app/UserVip/selectUserVip").then(res => {
					if (res.code == 0) {
						if (res.data && res.data.isVip) {
							this.isVip = res.data.isVip
						} else {
							this.isVip = -1
						}

						this.isVipEnd = res.data.endTime
					}
				});
			},
			// 我的金币
			getMoney() {
				this.$Request.get("/app/userMoney/selectMyMoney").then(res => {
					if (res.code == 0 && res.data) {
						this.money = res.data.money
					}
				});
			},
			select(e) {
				this.selNum = e.id
				this.price = e.money
			},
			pay() {
				console.log(this.money, this.newPrice)
				if (!this.selNum) {
					uni.showToast({
						title: '请选择开通会员类型？',
						icon: 'none'
					})
					return
				}
				if (this.money >= this.price) {
					let data = {
						id: this.selNum,
						type: '2'
					}
					this.$Request.get('/app/orders/generateOrder', data).then(res => {
						if (res.code == 0) {
							this.$Request.get('/app/userMoney/payTakingOrder', {
								orderId: res.data.ordersId
							}).then(res => {
								console.log(res.data)
								if (res.code == 0) {
									this.isVip = 1
									uni.showToast({
										title: '开通成功！'
									})
									this.getIsVip()
									setTimeout(function() {
										uni.navigateBack()
									}, 1000)
								} else {
									uni.showToast({
										title: res.msg
									})
								}
							})
						}
					})
				} else {
					this.content = '当前金币不足,请充值'
					this.popupshow = true

				}
			},
			sureT() {
				uni.navigateTo({
					url: '/my/wallet/index'
				})
				this.popupshow = false
			}
		},
		computed: {

		}
	}
</script>

<style>
	.bg {
		background-color: #291B3D;
	}

	.btn {
		width: 100%;
		height: 88upx;
		/* background: linear-gradient(0deg, #af8262 0%, #cab49c 100%); */
		background: #AC75FE;
		border-radius: 44upx;
		text-align: center;
		line-height: 88upx;
		margin-top: 40upx;
		font-size: 34upx;
		font-weight: 600;
		color: #FFFFFF;
	}

	.active {
		/* border: 1px solid #cab49c !important; */
		background: linear-gradient(-90deg, #552775, #28183e) !important;
		border: 1rpx solid #ac75fe !important;
		/* border-radius: ; */
	}

	.vipbox {
		padding: 30rpx 0;
		border-radius: 24rpx;
		margin: 30rpx 30rpx;
		background-image: url('https://pw.xianmxkj.com/file/uploadPath/2023/03/22/4954b77fd8b51a4e80da4f1c3d6d267e.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;
	}
</style>